This assignment use the Public Assistance Funded Projects Details dataset from FEMA. FEMA provides supplemental Federal disaster grant assistance for debris removal, emergency protective measures, and the repair, replacement, or restoration of disaster-damaged, publicly owned facilities and the facilities of certain Private Non-Profit (PNP) organizations through the PA Program. The data link: https://www.fema.gov/openfema-data-page/public-assistance-funded-projects-details-v1
#read data
public<- read.csv("Public.csv")
#get the year information
public$obligatedYear <- year(public$obligatedDate)
public$declarationYear <- year(public$declarationDate)
When the state code is less than 2 width, fill it up with “0”, such as “2” to “02”.
When the county code is less than 2 width, fill it up with “0”.
public$states <- formatC(public$stateNumberCode, width = 2, flag = 0)
public$countyc <- formatC(public$countyCode, width = 3, flag = 0)
#combine stateNumberCode and countyCode
public$fips<- str_c(public$states, public$countyc)
#filter the data into declarationYear between 2009-2018, get only the hurricane data
public.hurricane <- public %>% filter(incidentType ==c("Hurricane","Severe Storm(s)","Coastal Storm"))
public.hurricane%<>% filter(declarationYear>=2009 & declarationYear <=2018)
public.new<-public.hurricane %>% .[-which(.$county == "Statewide"),]
Summarize with each fips, delete the repeated lines of states and counties
display<- public.new %>% group_by(fips) %>% summarise(n=n(),state=unique(state),county=unique(county),incidentType=unique(incidentType),sumObligated=sum(totalObligated)) %>% rename(GEO_ID = fips)
## `summarise()` regrouping output by 'fips' (override with `.groups` argument)
#round the amount with thousands
display$sumObligated<- display$sumObligated/1000
head(display)
## # A tibble: 6 x 6
## # Groups: GEO_ID [5]
## GEO_ID n state county incidentType sumObligated
## <chr> <int> <chr> <chr> <chr> <dbl>
## 1 01001 13 Alabama Autauga Severe Storm(s) 191.
## 2 01003 77 Alabama Baldwin Severe Storm(s) 4940.
## 3 01003 77 Alabama Baldwin Hurricane 4940.
## 4 01005 24 Alabama Barbour Severe Storm(s) 210.
## 5 01007 10 Alabama Bibb Severe Storm(s) 444.
## 6 01009 13 Alabama Blount Severe Storm(s) 415.
-GEO_ID: fips code of the county
-state: state name
-county: county name
-sumObligated: Sum of the federal share of the Public Assistance grant eligible project amount in dollars, plus grantee (State) and sub-grantee (applicant) administrative costs between 2009-2018.
-n:numbers of the funded hurricane projects between 2009-2018
-incidentType: Type of incident:“Hurricane”,“Severe Storm(s)”,“Coastal Storm”
mapping of the funding amount for hurricane disasters
#get the layer of US map from the ".json" file as xy
#The data comes from JSON.
#We’ll use the geojsonio package to load the data from javascrpt into sp objects, which will let us easily manipulate the geographic features, and their properties, in R.
xy <- geojsonio::geojson_read("gz_2010_us_050_00_5m.json", what = "sp")
#names(xy)
#get the string between 10-14 digit of GEO_ID to match the 5-digit string of "fips"
xy$GEO_ID %<>% substr(start = 10, stop = 14)
# join the US map data with funding data by fips/GEO-ID
leafmap <- geo_join(xy, display, by = "GEO_ID", how = "inner")
#pal function:maps data values "sumObligated" to colors according to a given palette
#pal: colors for the fund amount of hurricanes
pal <- leaflet::colorNumeric(palette = "Blues", domain = leafmap$sumObligated)
#paln: colors for the number of hurricanes
paln<- leaflet::colorNumeric(palette = "Reds", domain = leafmap$n)
#add labels
labels <- sprintf(
"%s<br/>%s<br/>%s",
leafmap$state,leafmap$county, leafmap$sumObligated)
#add popups
popup<- paste0("<strong>state:</strong>", leafmap$state,"<br>",
"<strong>county:</strong>", leafmap$county,"<br>",
"<strong>Sum of Obligated Amount:</strong>",round(leafmap$sumObligated,2),"thousand")
popupn<- paste0("<strong>state:</strong>", leafmap$state,"<br>",
"<strong>county:</strong>", leafmap$county,"<br>",
"<strong>Number of funded hurricane projects:</strong>",leafmap$n)
#mapping by leaflet,showing the sum between 2009-2018
leaflet() %>%
#titles
addTiles() %>%
addProviderTiles("CartoDB.Positron") %>%
addProviderTiles(providers$Stamen.TonerLines,
options = providerTileOptions(opacity = 0.75)) %>%
setView(-89.275673, 37.098, zoom = 4) %>%
addPolygons(data = leafmap,
fillColor = ~pal(sumObligated),
color = "#BDBDC3",
fillOpacity = 1,
weight = 1,
smoothFactor = 0.2,
popup= popup,
popupOptions = popupOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")
) %>%
addLegend(pal = pal,
values = leafmap$sumObligated,
position = "bottomright",
title = paste0("Sum obligated amount(in thousands)<br>2009-2018"))
## mapping for the numbers of hurricanes funded projects
leaflet() %>%
#titles
addTiles() %>%
addProviderTiles("CartoDB.Positron") %>%
addProviderTiles(providers$Stamen.TonerLines,
options = providerTileOptions(opacity = 0.75)) %>%
setView(-89.275673, 37.098, zoom = 4) %>%
addPolygons(data = leafmap,
fillColor = ~paln(n),
color = "#BDBDC3",
fillOpacity = 1,
weight = 1,
smoothFactor = 0.2,
popup= popupn,
popupOptions = popupOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")
) %>%
addLegend(pal = paln,
values = leafmap$n,
position = "bottomright",
title = paste0("Numbers of funded hurricane projects<br>2009-2018"))
[1]Garrett Grolemund, Hadley Wickham (2011). Dates and Times Made Easy with lubridate. Journal of Statistical Software, 40(3), 1-25. URL http://www.jstatsoft.org/v40/i03/.
[2]Hadley Wickham (2019). stringr: Simple, Consistent Wrappers for Common String Operations. R package version 1.4.0. https://CRAN.R-project.org/package=stringr
[3]Hadley Wickham, Romain François, Lionel Henry and Kirill Müller (2020). dplyr: A Grammar of Data Manipulation. R package version 1.0.2. https://CRAN.R-project.org/package=dplyr
[4]Stefan Milton Bache and Hadley Wickham (2014). magrittr: A Forward-Pipe Operator for R. R package version 1.5. https://CRAN.R-project.org/package=magrittr
[5]Kyle Walker (2020). tigris: Load Census TIGER/Line Shapefiles. R package version 1.0. https://CRAN.R-project.org/package=tigris
[6]Roger Bivand, Tim Keitt and Barry Rowlingson (2020). rgdal: Bindings for the ‘Geospatial’ Data Abstraction Library. R package version 1.5-18. https://CRAN.R-project.org/package=rgdal
[7]Scott Chamberlain and Andy Teucher (2020). geojsonio: Convert Data from and to ‘GeoJSON’ or ‘TopoJSON’. R package version 0.9.2. https://CRAN.R-project.org/package=geojsonio
[8]Joe Cheng, Bhaskar Karambelkar and Yihui Xie (2019). leaflet: Create Interactive Web Maps with the JavaScript ‘Leaflet’ Library. R package version 2.0.3. https://CRAN.R-project.org/package=leaflet